{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2acaf51b",
   "metadata": {},
   "source": [
    "* 本学习记录人：许智超\n",
    "* 学习时间：2021-11-30\n",
    "* week14\n",
    "* 参考的文档：\n",
    "> 1.[Python官方文档](https://python.org/doc)\n",
    "\n",
    "> 2.[许老师博客](https://cnblogs.com/xuzhichao)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "308f9504",
   "metadata": {},
   "source": [
    "# 本周重点知识\n",
    "\n",
    "## 1. 初始字典\n",
    "## 2. 字典的一般创建方法\n",
    "## 3. 字典的操作\n",
    "## 4. 实践"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2c5405f1",
   "metadata": {},
   "source": [
    "> 1.序列数据 index 是 从0开始，每次加一的 数值作为索引值 [88,87,90]\n",
    "\n",
    "> 2.映射数据 index 是 关键字（不可变数据，例如字符串，数字，元组）\n",
    "\n",
    "> 3.序列数据是 有顺序的\n",
    "\n",
    "> 4.映射数据是 无序的\n",
    "\n",
    "> 5.序列数据 index 是不写在数据值中的，不显示的\n",
    "\n",
    "> 6.映射数据 index 作为 关键字（key）写在数据结构中 ： {key:value} 这种形式通常被人们称为 键值对（key: value pairs"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cfed93e0",
   "metadata": {},
   "source": [
    "* 总结字典的性质："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c9aefe2",
   "metadata": {},
   "source": [
    "> 1.字典的key 是唯一的 (唯一性)\n",
    "\n",
    "> 2.字典是没有顺序的（无序性）\n",
    "\n",
    "> 3.字典的key是不可变的(不变性)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "e7ea128a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 初识字典\n",
    "dic_name = {39: 'Miku', 21: 'Master'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "286a6860",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{39: 'Miku', 21: 'Master'}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dic_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "8bb1647f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Miku'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dic_name[39]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "add957d0",
   "metadata": {},
   "source": [
    "# 2.字典的一般创建方法\n",
    "\n",
    "## 按照字典的数据结构直接创建\n",
    "> * dict_name = {key1:value1 , key2:value2 , key3:value3 , ...}\n",
    "\n",
    "## 使用dict()方法创建字典\n",
    "> * dict_name = dict(key1=value1 , key2=value2 , key3=value3 , ...)\n",
    "\n",
    "## 使用字典新增的方式创建字典（常用）\n",
    "* dict_name[key1] = value1 , dict_name[key2] = value2 , dict_name[key3] = value3 ,..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "1a0d9f9d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# **注意： 冒号和逗号都是英文的**\n",
    "stud_lis = {\"01\": \"Miku\",\n",
    "            \"02\": \"Master\",\n",
    "           \"03\": \"Gumi\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "93769b28",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'>\n",
      "{'01': 'Miku', '02': 'Master', '03': 'Gumi'}\n"
     ]
    }
   ],
   "source": [
    "print(type(stud_lis))\n",
    "print(stud_lis)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "996a24e8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Miku'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stud_lis[\"01\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f9b9903c",
   "metadata": {},
   "source": [
    "* 练习查询 https://httpbin.org/get"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "5461dded",
   "metadata": {},
   "outputs": [],
   "source": [
    "get_dict = {\n",
    "  \"args\": {}, \n",
    "  \"headers\": {\n",
    "    \"Accept\": \"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\", \n",
    "    \"Accept-Encoding\": \"gzip, deflate, br\", \n",
    "    \"Accept-Language\": \"zh-CN,zh;q=0.9\", \n",
    "    \"Host\": \"httpbin.org\", \n",
    "    \"Sec-Ch-Ua\": \"\\\"Chromium\\\";v=\\\"92\\\", \\\" Not A;Brand\\\";v=\\\"99\\\", \\\"Google Chrome\\\";v=\\\"92\\\"\", \n",
    "    \"Sec-Ch-Ua-Mobile\": \"?0\", \n",
    "    \"Sec-Fetch-Dest\": \"document\", \n",
    "    \"Sec-Fetch-Mode\": \"navigate\", \n",
    "    \"Sec-Fetch-Site\": \"none\", \n",
    "    \"Sec-Fetch-User\": \"?1\", \n",
    "    \"Upgrade-Insecure-Requests\": \"1\", \n",
    "    \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36\", \n",
    "    \"X-Amzn-Trace-Id\": \"Root=1-61a58fbf-7314033011179e850449460a\"\n",
    "  }, \n",
    "  \"origin\": \"210.21.79.254\", \n",
    "  \"url\": \"https://httpbin.org/get\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "d93f0157",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'zh-CN,zh;q=0.9'"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_dict[\"headers\"][\"Accept-Language\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "295362e7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2. 使用dict()方法创建字典(局限性： key 是以 变量名的方式进行赋值的)\n",
    "stud_lis2 = dict(\n",
    "     name1 = \"Miku\",\n",
    "     name2 = \"Master\",\n",
    "     name3 = \"Gumi\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "98140d77",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name1': 'Miku', 'name2': 'Master', 'name3': 'Gumi'}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stud_lis2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "46525bae",
   "metadata": {},
   "outputs": [],
   "source": [
    "stud_lis3 ={}\n",
    "stud_lis3['01'] = 'Miku'\n",
    "stud_lis3['02'] = 'Master'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "100725bb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'01': 'Miku', '02': 'Master'}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stud_lis3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e6e9ef19",
   "metadata": {},
   "source": [
    "# 3. 字典的操作"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2804185c",
   "metadata": {},
   "source": [
    "> 1.3. 字典的操作\n",
    "\n",
    ">> 1.字典的查询\n",
    "\n",
    ">> 2.字典的增加\n",
    "\n",
    ">> 3.字典的修改\n",
    "\n",
    ">> 4.字典的删除\n",
    "\n",
    "> 2.字典的一般方法\n",
    "\n",
    ">> 1.查询"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "70373f25",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "03不存在\n"
     ]
    }
   ],
   "source": [
    "# 1.字典的查询 方法1\n",
    "if '03' in stud_lis3:\n",
    "    print(stud_lis3['03'])\n",
    "else:\n",
    "    print('03'+\"不存在\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "476558f5",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'03'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-30-d9b0b3280f39>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m# 如果查询不存在： KeyError: '03'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstud_lis3\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'03'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m: '03'"
     ]
    }
   ],
   "source": [
    "# 如果查询不存在： KeyError: '03'\n",
    "print(stud_lis3['03'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "f4a10f56",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "# 2.字典的查询 方法2 get()\n",
    "print(stud_lis3.get('03'))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2ca22389",
   "metadata": {},
   "source": [
    "> 2.字典的增加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "b8586e0f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "print(stud_lis3.get('03'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "d929a149",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入一个新的学生的名字Gumi\n"
     ]
    }
   ],
   "source": [
    "if stud_lis3.get('03')== None:\n",
    "    stud_lis3['03'] = input(\"输入一个新的学生的名字\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "5f1527d3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'01': 'Miku', '02': 'Master', '03': 'Gumi'}"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stud_lis3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0ebdfec",
   "metadata": {},
   "source": [
    "> 4.字典的删除\n",
    "\n",
    ">> 1.pop(): 指定key删除\n",
    "\n",
    ">>> 1.D.pop(k[,d])\n",
    "\n",
    ">>> 2.remove specified key（指定的key关键字） and return the corresponding value（返回删除的value值）\n",
    "\n",
    ">> 2.popitem()：随机删除vvvv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "884af25a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'01': 'Miku', '02': 'Master', '03': 'Gumi'}"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stud_lis3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "6a363c0c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(...) method of builtins.dict instance\n",
      "    D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n",
      "    If key is not found, d is returned if given, otherwise KeyError is raised\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(stud_lis3.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "8efbf241",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Gumi'"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 因为字典是无序的，所以没有最后一个元素，pop指定关键字删除\n",
    "stud_lis3.pop('03')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "be622214",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'01': 'Miku', '02': 'Master'}"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stud_lis3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "4790d6cc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function popitem:\n",
      "\n",
      "popitem(...) method of builtins.dict instance\n",
      "    D.popitem() -> (k, v), remove and return some (key, value) pair as a\n",
      "    2-tuple; but raise KeyError if D is empty.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(stud_lis3.popitem)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "b50e5c6c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('02', 'Master')"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stud_lis3.popitem()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "7d2d64ea",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "按a查看学生名单，按b添加学生，按c删除学生，按d搜索学生，按e退出\n",
      "输入字母d\n",
      "好的\n",
      "请输入查询学生学号：01\n",
      "好的\n",
      "Miku\n",
      "按a查看学生名单，按b添加学生，按c删除学生，按d搜索学生，按e退出\n",
      "输入字母a\n",
      "{'01': 'Miku', '02': 'Master'}\n",
      "按a查看学生名单，按b添加学生，按c删除学生，按d搜索学生，按e退出\n",
      "输入字母b\n",
      "请输入想添加的学生名称：Gumi\n",
      "好的\n",
      "请输入学生的学号：03\n",
      "{'01': 'Miku', '02': 'Master', '03': 'Gumi'}\n",
      "按a查看学生名单，按b添加学生，按c删除学生，按d搜索学生，按e退出\n",
      "输入字母c\n",
      "好的，第几位学生？请输入学号：03\n",
      "{'01': 'Miku', '02': 'Master'}\n",
      "按a查看学生名单，按b添加学生，按c删除学生，按d搜索学生，按e退出\n",
      "输入字母e\n",
      "好的，再见，欢迎下次使用~\n"
     ]
    }
   ],
   "source": [
    "# 这里可以与先前的学生名单查询功能进行对比\n",
    "stu_name = {'01': 'Miku',\n",
    "           '02': 'Master'}\n",
    "while True:\n",
    "    print(\"按a查看学生名单，按b添加学生，按c删除学生，按d搜索学生，按e退出\")\n",
    "    answer = input(\"输入字母\")\n",
    "    if answer == 'a':\n",
    "        print(stu_name)\n",
    "    elif answer == 'b':\n",
    "        name = input(\"请输入想添加的学生名称：\")\n",
    "        if len(name) == 0:\n",
    "            print(\"输入有误，字符串不能是空的\")\n",
    "        else:\n",
    "            print(\"好的\")\n",
    "            nam = name.replace(' ', \"\")\n",
    "            num = input(\"请输入学生的学号：\")\n",
    "            if num.isdigit() is False:\n",
    "                print(\"输入有误，请重头再来\")\n",
    "            stu_name[num] = nam\n",
    "            print(stu_name)\n",
    "    elif answer == 'c':\n",
    "        dele = input(\"好的，第几位学生？请输入学号：\")\n",
    "        if dele.isdigit() is True:\n",
    "            stu_name.pop(dele)\n",
    "            print(stu_name)\n",
    "        else:\n",
    "            print(\"输入有误，只输入阿拉伯数字，再来一遍！\")\n",
    "    elif answer == 'd':\n",
    "        print(\"好的\")\n",
    "        whois = input(\"请输入查询学生学号：\")\n",
    "        if stu_name.get(whois) == None:\n",
    "            print(\"不好意思，这并不存在呢，请待会按b添加该学生\")\n",
    "        else:\n",
    "            print(\"好的\")\n",
    "            print(stu_name[whois])\n",
    "    elif answer == 'e':\n",
    "        print(\"好的，再见，欢迎下次使用~\")\n",
    "        break\n",
    "    else:\n",
    "        print(\"输入格式有误，应该输入字母\")\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a1e9cba2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
